package com.itheima.ydd.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.ydd.common.R;
import com.itheima.ydd.common.UserHolder;
import com.itheima.ydd.dto.ConfigDto;
import com.itheima.ydd.entity.*;
import com.itheima.ydd.mapper.SysConfigMapper;
import com.itheima.ydd.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yan.zhou
 * @since 2022-02-21
 */
@Service
public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig> implements ISysConfigService {

    @Autowired
    private ISysUserService userService;
    @Autowired
    private ISysRoleService roleService;
    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysConfigMapper configMapper;
/*

    @Override
    public Map<String, Object> profile() {
        HashMap<String, Object> map = new HashMap<>();
        //1. 查询用户信息
        String userId = UserHolder.getUserId();
        User user = userService.getById(UserHolder.getUserId());
        SysUserInfo userInfo = new SysUserInfo();
        BeanUtils.copyProperties(user, userInfo);

        //查询用户角色id集合: role_id
        String[] rowIds = user.getRole_id().replace("[", "")
                .replace("]", "")
                .replace("\"", "")
                .split(",");
        List<String> ids = new ArrayList<>();
        Collections.addAll(ids, rowIds);
        userInfo.setRole_id(ids);
        map.put("userInfo", userInfo);

        //查询用户角色名称集合: role_id
        String role_id = UserHolder.get().getRole_id();
        List<String> roleNameList = roleService.roleNameList(role_id);
        userInfo.setRolename(roleNameList);

        //2. 查询角色菜单集合
        List<Menu> menuList = menuService.menuList(role_id);
        map.put("menuList", menuList);


        //region #dictsList 不知道干嘛用的，结果直接为原本数据
        HashMap<String, Object> dictsList = new HashMap<>();
        HashMap<String, String> interface_type = new HashMap<>();
        interface_type.put("get", "get");
        interface_type.put("post", "post");
        dictsList.put("interface_type", interface_type);

        ArrayList<String> table_type = new ArrayList<>();
        table_type.add("单表");
        table_type.add("主表");
        table_type.add("附表");
        table_type.add("树结构表");
        dictsList.put("table_type", table_type);

        ArrayList<String> order_type = new ArrayList<>();
        order_type.add("支付宝");
        order_type.add("微信");
        dictsList.put("order_type", order_type);

        ArrayList<String> report_type = new ArrayList<>();
        report_type.add("正常报告");
        report_type.add("异常报告");
        dictsList.put("report_type", report_type);

        HashMap<String, String> theme = new HashMap<>();
        theme.put("ace", "ACE风格");
        dictsList.put("theme", theme);
        map.put("dictsList", dictsList);

        return map;
    }
*/

    @Override
    public R getAll() {

        List<SysConfig> configs = configMapper.selectList(new LambdaQueryWrapper<SysConfig>()
                .orderByAsc(SysConfig::getSortNum));

        Map<String, String> configForm = new HashMap<>();
        Map<String, String> configRules = new HashMap<>();

        List<ConfigDto> dtoList = configs.stream().map((config) -> {
            ConfigDto dto = new ConfigDto();
            BeanUtils.copyProperties(config, dto);

            //处理 allData
            doConfigValue(dto, config);

            // 处理 configForm
            configForm.put(config.getAttrKey(), config.getAttrValue());

            // 处理configRules
            Map<String, String> rules = doConfigRules(config);
            BeanUtils.copyProperties(rules, configRules);

            return dto;
        }).collect(Collectors.toList());


        Map map1=new HashMap();

        map1.put("site_name","[{\"required\":true,\"message\":\"站点名必须\",\"trigger\":\"blur\"}]");


        //TODO




        ListConfig config1 = new ListConfig();
        config1.setName("基础配置");
        config1.setKey("base");
        LambdaQueryWrapper<SysConfig> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysConfig::getGroupValue, "站点配置");
        List<SysConfig> configs1 = this.list(lambdaQueryWrapper);
       /* map1.put("站点配置",configs1);
        config1.getChildren().add(map1);*/
        ClassChildren children1 = new ClassChildren();
        children1.setData(configs1);
        children1.setName("站点配置");


        LambdaQueryWrapper<SysConfig> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.eq(SysConfig::getGroupValue, "底部信息");
        List<SysConfig> configs2 = this.list(lambdaQueryWrapper1);
       /* Map<String,Object> map2=new HashMap<>();
        map2.put("底部信息",configs2);
        config1.getChildren().add(map2);*/
        ClassChildren children2 = new ClassChildren();
        children2.setData(configs2);
        children2.setName("底部信息");


        LambdaQueryWrapper<SysConfig> lambdaQueryWrapper2 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper2.eq(SysConfig::getGroupValue, "其他配置");
        List<SysConfig> configs3 = this.list(lambdaQueryWrapper2);
        /*Map<String,Object> map3=new HashMap<>();
        map3.put("其他配置",configs3);
        config1.getChildren().add(map3);*/
        ClassChildren children3 = new ClassChildren();
        children3.setData(configs3);
        children3.setName("其他配置");

        List<ClassChildren> childrenList = new ArrayList<>();
        childrenList.add(children1);
        childrenList.add(children2);
        childrenList.add(children3);
        config1.setChildren(childrenList);


        ListConfig config2 = new ListConfig();
        config2.setName("系统设置");
        config2.setKey("system");
        LambdaQueryWrapper<SysConfig> lambdaQueryWrapper3 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper3.eq(SysConfig::getGroupValue, "管理系统配置");
        List<SysConfig> configs4 = this.list(lambdaQueryWrapper3);
        ClassChildren children4 = new ClassChildren();
        children4.setData(configs4);
        children4.setName("管理系统配置");
        List<ClassChildren> childrenList2 = new ArrayList<>();
        childrenList2.add(children4);
        config2.setChildren(childrenList2);


        ListConfig config3 = new ListConfig();
        config3.setName("其他设置");
        config3.setKey("custom");
        LambdaQueryWrapper<SysConfig> lambdaQueryWrapper4 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper4.eq(SysConfig::getGroupValue, "通知公告");
        List<SysConfig> configs5 = this.list(lambdaQueryWrapper4);
        ClassChildren children5 = new ClassChildren();
        children5.setData(configs5);
        children5.setName("通知公告");

        LambdaQueryWrapper<SysConfig> lambdaQueryWrapper5 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper5.eq(SysConfig::getGroupValue, "前台图片");
        List<SysConfig> configs6 = this.list(lambdaQueryWrapper4);
        ClassChildren children6 = new ClassChildren();
        children6.setData(configs6);
        children6.setName("前台图片");

        List<ClassChildren> childrenList3 = new ArrayList<>();
        childrenList3.add(children5);
        childrenList3.add(children6);
        config3.setChildren(childrenList3);


        List<Object> list = new ArrayList<>();
        list.add(config1);
        list.add(config2);
        list.add(config3);


        R r = new R();
        r.putData("allData",dtoList);
        r.putData("configForm", configForm);
        r.putData("list", list);
        r.putData("configRules", map1);



        r.setErrno(0);
        r.setErrmsg("");
        return r;

        //return R.success("",map);
    }

    private Map<String, String> doConfigRules(SysConfig config) {
        Map<String, String> ruleHashMap = new HashMap<>();
        Integer type = config.getType();
        if (type == 4) {
            String[] rules = config.getConfigValue().split("&");
            for (String rule : rules) {
                String[] split = rule.split("=");
                ruleHashMap.put("label", split[0]);
                ruleHashMap.put("value", split[1]);
            }
        }
        return ruleHashMap;
    }

    /**
     * 处理 ConfigValue
     *
     * @param dto
     * @param config
     * @return
     */
    public ConfigDto doConfigValue(ConfigDto dto, SysConfig config) {

        String config_value = config.getConfigValue();
        if (config_value != null && !"".equals(config_value)) {
            Integer type = config.getType();
            if (type == 4) {
                String[] rules = config_value.split("&");
                List<Map<String, String>> configValue = new ArrayList<>();
                for (String rule : rules) {
                    String[] split = rule.split("=");
                    Map<String, String> ruleHashMap = new HashMap<>();

                    ruleHashMap.put("label", split[0]);
                    ruleHashMap.put("value", split[1]);
                    configValue.add(ruleHashMap);
                }
                dto.setConfig_value(configValue);

                return dto;
            }
        }

        return null;
    }

}
